package com.kayt.cyberBar.cuser.business.server.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kayt.cyberBar.common.GenPublicCode;
import com.kayt.cyberBar.common.utils.TimeUtils;
import com.kayt.cyberBar.cuser.business.dao.IExamQuotaDao;
import com.kayt.cyberBar.cuser.business.entity.po.BExamQuota;
import com.kayt.cyberBar.cuser.business.server.IExamQuotaServer;

@Service("examQuotaServer")
public class ExamQuotaServerImpl implements IExamQuotaServer {

	@Autowired
	private IExamQuotaDao examQuotaDao;

	@Override
	public BExamQuota findByStoreCode(String storeCode) {
		try {
			BExamQuota examQuota = examQuotaDao.findByStoreCode(storeCode);
			if(examQuota == null){
				examQuota = new BExamQuota();
				examQuota.setCode(GenPublicCode.genWorkReuqst());
				examQuota.setExamQuota("500");
				examQuota.setStoreCode(storeCode);
				examQuota.setUpdateTime(TimeUtils.generateTimeStr());
				examQuota.setCreateTime(TimeUtils.generateTimeStr());
				examQuotaDao.saveOrUpate(examQuota);
			}
			return examQuota;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	@Override
	public Integer saveOrUpdate(String storeCode, String quota) {
		try{
			BExamQuota examQuota = examQuotaDao.findByStoreCode(storeCode);
			if(examQuota == null){
				examQuota = new BExamQuota();
				examQuota.setStoreCode(storeCode);
				examQuota.setCode(GenPublicCode.genWorkReuqst());
				examQuota.setCreateTime(TimeUtils.generateTimeStr());
			}
			examQuota.setExamQuota(quota);
			examQuota.setUpdateTime(TimeUtils.generateTimeStr());
			examQuotaDao.saveOrUpate(examQuota);
			return 0;
		}catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

}
